package com.link.base.base.mptmpl.controller;

import com.link.base.base.mptmpl.model.MpTmpl;
import com.link.base.base.mptmpl.service.MpTmplService;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.KeyGenerateService;
import com.link.core.cllog.LCLogger;
import com.link.core.util.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 小程序页面模板配置
 *
 * @author 黄安邦
 * @date 2020-07-02 18:27:01
 */
@Controller
@RequestMapping("/link/mpTmpl")
public class MpTmplController extends BasicController<MpTmpl> {

    @Resource
    private KeyGenerateService keyGenerateService;

    @Resource
    private MpTmplService mpTmplService;

    @Override
    public BasicService<MpTmpl> getBasicService() throws Exception {
        return mpTmplService;
    }

    /**
     * 根据模板类型获取当前用户有权访问的模板，并返回模板是json字符串
     *
     * @param type
     * @param request
     * @param response
     * @return
     * @author 姚润发
     * @date 2020/7/29 16:23
     */
    @RequestMapping({"/get/{type}"})
    @ResponseBody
    public Map<String, Object> getTmplByType(@PathVariable String type, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<>();
        try {
            if (StringUtils.isBlank(type)) {
                result.put("success", false);
                result.put("result", "模板类型不能为空");
                return result;
            }
            MpTmpl example = new MpTmpl();
            example.setActiveFlag("Y");
            example.setType(type);
            example.setRows(1000);
            String tmplJson = mpTmplService.getTmplByRule(example);
            if (tmplJson == null) {
                result.put("success", false);
                result.put("result", "当前用户没有该模板权限");
                return result;
            }
            result.put("success", true);
            result.put("result", tmplJson);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 根据模板类型，清除该类型所有缓存
     *
     * @param type
     * @param request
     * @param response
     * @return
     * @author 姚润发
     * @date 2020/7/29 16:28
     */
    @RequestMapping({"/clear/{type}"})
    @ResponseBody
    public Map<String, Object> clearTmplCacheByType(@PathVariable String type, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<>();
        try {
            if (StringUtils.isBlank(type)) {
                result.put("success", false);
                result.put("result", "模板类型不能为空");
                return result;
            }
            mpTmplService.clearCacheByType(type);
            result.put("success", true);
            result.put("result", null);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }
}
